<?php	
	// clean up the input

	if(empty($_GET['name']))	{header ("Location: http://coca.vndes.net/");} ;
	
	$text = html_entity_decode($_GET['name']).' ' ;
	if(empty($text))
	{
		fatal_error('Error: Text not properly formatted.') ;
		}
	// customizable variables
	$font_file 		= 'Coke4U.ttf';

	if( strlen(utf8_decode($_GET['name'])) > 8 AND strlen(utf8_decode($_GET['name'])) < 13 )
	{
	$font_size  	= 47;
	}
	elseif( strlen(utf8_decode($_GET['name'])) > 13 AND strlen(utf8_decode($_GET['name'])) < 18 )
	{
	$font_size  	= 35;
	}
	elseif( strlen(utf8_decode($_GET['name'])) > 18 AND strlen(utf8_decode($_GET['name'])) < 23 )
	{
	$font_size  	= 28;
	}
	else {
	$font_size  	= 60;
	}
	$x_finalpos 	= 190;
	$y_finalpos 	= 440;
	$font_color 	= '#ffffff' ;
	$image_file 	= 'img_dung.png';
	// x and y for the bottom right of the text
	// so it expands like right aligned text
	
	// trust me for now...in PNG out PNG
	$mime_type 			= 'image/png' ;
	$extension 			= '.png' ;
	$s_end_buffer_size 	= 4096 ;
	// check for GD support
	if(!function_exists('ImageCreate'))
		fatal_error('Error: Server does not support PHP image generation') ;
	// check font availability;
	if(!is_readable($font_file)) {
		fatal_error('Error: The server is missing the specified font.') ;
	}
	// create and measure the text
	$font_rgb = hex_to_rgb($font_color) ;
	$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;	
	$text_width = abs($box[2]-$box[0]);
	$text_height = abs($box[5]-$box[3]);	
	$image =  imagecreatefrompng($image_file);

	
	if(!$image || !$box)

	{

		fatal_error('Error: The server could not create this image.') ;

	}

	

	// allocate colors and measure final text position

	$font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;

	

	$image_width = imagesx($image);
	//$image_height = imagesy($image);
	

	$put_text_x = $x_finalpos;

	$put_text_y =$y_finalpos;



	// Write the text

	imagettftext($image, $font_size, 90, $put_text_x,  $put_text_y, $font_color, $font_file, $text);

	header('Content-type: ' . $mime_type) ;

	$image_ok = $image;
//imagedestroy($image);
	$stamp = imagecreatefrompng('water_dung.png');
	imagefilter($stamp, IMG_FILTER_BRIGHTNESS, 3);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 70;
$marge_bottom = 65;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
	imagecopy($image_ok, $stamp, imagesx($image_ok) - $sx - $marge_right, imagesy($image_ok) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));	
	imagepng($image_ok);
	imagedestroy($image_ok);

	exit ;



	

	/*

		attempt to create an image containing the error message given. 

		if this works, the image is sent to the browser. if not, an error

		is logged, and passed back to the browser as a 500 code instead.

	*/

	function fatal_error($message)

	{

		// send an image

		if(function_exists('ImageCreate'))

		{

			$width = ImageFontWidth(5) * strlen($message) + 10 ;

			$height = ImageFontHeight(5) + 10 ;

			if($image = ImageCreate($width,$height))

			{

				$background = ImageColorAllocate($image,255,255,255) ;

				$text_color = ImageColorAllocate($image,0,0,0) ;

				ImageString($image,5,5,5,$message,$text_color) ;    

				header('Content-type: image/png') ;

				ImagePNG($image) ;

				ImageDestroy($image) ;

				exit ;

			}

		}

	

		// send 500 code

		header("HTTP/1.0 500 Internal Server Error") ;

		print($message) ;

		exit ;

	}

	

	

	/* 

		decode an HTML hex-code into an array of R,G, and B values.

		accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff 

	*/    

	function hex_to_rgb($hex) {

		// remove '#'

		if(substr($hex,0,1) == '#')

			$hex = substr($hex,1) ;

	

		// expand short form ('fff') color to long form ('ffffff')

		if(strlen($hex) == 3) {

			$hex = substr($hex,0,1) . substr($hex,0,1) .

				   substr($hex,1,1) . substr($hex,1,1) .

				   substr($hex,2,1) . substr($hex,2,1) ;

		}

	

		if(strlen($hex) != 6)

			fatal_error('Error: Invalid color "'.$hex.'"') ;

	

		// convert from hexidecimal number systems

		$rgb['red'] = hexdec(substr($hex,0,2)) ;

		$rgb['green'] = hexdec(substr($hex,2,2)) ;

		$rgb['blue'] = hexdec(substr($hex,4,2)) ;

	

		return $rgb ;

	}

?>
